package com.edushare.common.vo;

import java.util.Date;
import java.util.List;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.file.AssignmentFileProcessingStrategy;
import com.edushare.common.file.IFileProcessStrategy;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.util.DateTimeUtil;
import com.edushare.common.util.StringUtil;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.FileDirectories.DIRECTORY_OWNER_TYPE;
import com.edushare.common.vo.auto._Assignments;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class Assignments extends _Assignments implements IFileUploadable, DateComparision 
{

	private static final long serialVersionUID = 7034623451989707838L;
	
	public static final int SORT_USERS_BY_NAME 					= 1;
	public static final String SORT_BY_STATUS 					= Assignments.STATUS_PROPERTY;
	
	public static final int ASSIGNMENT_STATUS_CLOSED			= 10;
	public static final int ASSIGNMENT_STATUS_DRAFT 			= 50;
	public static final int ASSIGNMENT_STATUS_LATE				= 75;
	public static final int ASSIGNMENT_STATUS_OPEN				= 100;
	
	public static final int ASSIGNMENT_PRIVACY_PRIVATE			= 10;
	public static final int ASSIGNMENT_PRIVACY_PUBLIC			= 20;
	
	public static final int ASSIGNMENT_ALLOW_LATE_SUBMIT		= 10;
	public static final int ASSIGNMENT_NO_LATE_SUBMIT			= 20;
	
	private int fileUploadPrivacy = ApplicationConstants.FILE_DIR_PERMISSION_TYPE_PUBLIC;
	
	
	public static final String getStatusAsString(Integer statusValue){
		if(statusValue==null)
			return "N/A";
		switch(statusValue){
			case ASSIGNMENT_STATUS_CLOSED: return "Submission Closed";
			case ASSIGNMENT_STATUS_OPEN: return "Submission Open";
			case ASSIGNMENT_STATUS_LATE: return "Late Submission";
			case ASSIGNMENT_STATUS_DRAFT: return "Draft";
		}
		return "N/A";
	}
	
	public String getStatusAsString(){
		return getStatusAsString(this.getStatus());
	}

	public String getFormattedStartDate(){
			return DateTimeUtil.formatDate(getStartDate());
	}
	
	public String getFormattedEndDate(){
		return DateTimeUtil.formatDate(getEndDate());
	}
	
	public String getFormattedLateSubmDate(){
		return DateTimeUtil.formatDate(getLateSubmDate());
	}
	
	@Override
	public String getAssignmentTitle() {
		return StringUtil.isValid(super.getAssignmentTitle())?super.getAssignmentTitle():"[No Title]";
	}

	/** IFileUploadable implementation starts 
	 * NOTE: setFilePath, getFilePath are already implemented in super
	 */
	
	public List<String> getAllowedFileExtentions() {
		return null;
	}

	public Integer getMaxFileSizeInKb() {
		return Integer.parseInt(SystemGlobals.getInstance().
									getConfigProperty(ConfigPropertyName.UPLOAD_ASSIGNMENT_MAXSIZE_KB));
	}

	public String getSeoFileName() {
		return null;
	}
	
	public IFileProcessStrategy getFileProcessingStrategy() {
		return new AssignmentFileProcessingStrategy();
	}	

	public String getUploadedFileUrl() {
		return URLUtils.getImageUrlFromRelativePath(getFilePath());
	}
	
	public DIRECTORY_OWNER_TYPE getObjectType() {
		return DIRECTORY_OWNER_TYPE.ASSIGNMENT;
	}
	
	public int getObjectPrimaryKey() {
		return super.getAssignmentId();
	}

	public String getFilePath() {
		return null;
	}

	public void setFilePath(String filePath) {
	}

	public int getFileUploadPrivacyLevel() {
		return this.fileUploadPrivacy;
	}

	public void setFileUploadPrivacyLevel(int fileUploadPrivalyLevel) {
		this.fileUploadPrivacy = fileUploadPrivalyLevel;
	}


	/** IFileUploadable implementation ends */
	
	/*private boolean isSubmissionDatevalid() {
		
		if(		this.getStartDate()!=null && 
				this.getEndDate() !=null &&
				this.getStartDate().before(this.getEndDate()) &&
				new Date().after(this.getStartDate()) &&
				new Date().before(this.getEndDate())
			)
		{
			return true;
		}
			
		return false;
	}*/


	public boolean isSubmissionAllowed() {
		return (getStatus()==ASSIGNMENT_STATUS_OPEN || getStatus()==ASSIGNMENT_STATUS_LATE);
	}

	public String getTitle() {
		return getTitle();
	}

	public int compareTo(IDateComparable arg0) {
		if(getDate().getTime() < arg0.getDate().getTime())
			return -1;
		else if(arg0.getDate().getTime() == getDate().getTime())
			return 0;
		else
			return 1;
	}

	public Date getDate() {
		return getEndDate();
	}
	
	/***
	 * ===== For scheduler test purposes only do not uncomment if not testing 
	@Override
	public void setEndDate(Date endDate) {
		super.setEndDate(new Date(new Date().getTime()+1000*60*5));
	}
	
	@Override
	public void setLateSubmDate(Date lateSubmDate) {
		super.setLateSubmDate(new Date(new Date().getTime()+1000*60*10));
	}
	
	*/
}
